/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.doctor.entity.PlaceInfectionRecord;
import com.jims.master.modules.doctor.dao.PlaceInfectionRecordDao;

/**
 * Service
 * @author 系统管理员
 * @version 2017-07-19
 */
@Service
@Transactional(readOnly = true)
public class PlaceInfectionRecordService extends CrudService<PlaceInfectionRecordDao, PlaceInfectionRecord> {
	@Autowired
	PatMasterIndexDao patMasterIndexDao;
	@Autowired
	ClinicMasterDao clinicMasterDao;
	public PlaceInfectionRecord get(String id) {
		return super.get(id);
	}
	
	public List<PlaceInfectionRecord> findList(PlaceInfectionRecord placeInfectionRecord) {
		return super.findList(placeInfectionRecord);
	}
	
	public Page<PlaceInfectionRecord> findPage(Page<PlaceInfectionRecord> page, PlaceInfectionRecord placeInfectionRecord) {
		return super.findPage(page, placeInfectionRecord);
	}
	
	@Transactional(readOnly = false)
	public int save(PlaceInfectionRecord placeInfectionRecord) {
		User user = UserUtils.getUser();
		if(placeInfectionRecord.getPatMasterIndex()!=null&& StringUtils.isNotBlank(placeInfectionRecord.getPatMasterIndex().getId())){
			placeInfectionRecord.getPatMasterIndex().preUpdate();
			placeInfectionRecord.getPatMasterIndex().setSex(placeInfectionRecord.getSex());
			patMasterIndexDao.updateByInfection(placeInfectionRecord.getPatMasterIndex());
		}
		if(placeInfectionRecord.getInfectionNo()==null){
			int max=Integer.parseInt(dao.findMaxInfectionNo()) + 1;
			String maxDeathNo1= String.format("%017d", max);
			placeInfectionRecord.setInfectionNo(maxDeathNo1);//死亡编号按规律计算
		}
		placeInfectionRecord.setOrgId(user.getOrgId());
		return super.save(placeInfectionRecord);
	}
	
	@Transactional(readOnly = false)
	public int delete(PlaceInfectionRecord placeInfectionRecord) {
		return super.delete(placeInfectionRecord);
	}
	public List<PlaceInfectionRecord> findByConditions(String clinicId,String patientId){
		return dao.findByConditions(clinicId, patientId);
	}
	public PlaceInfectionRecord findByCondition(String clinicId,String patientId){
		return dao.findByCondition(clinicId, patientId);
	}
	/**
	 * 传染病打印
	 * @param clinicId
	 * @param patientId
	 * @return String
	 * @author 樊多
	 */
	public String print(String id,String clinicId,String patientId){
		User user = UserUtils.getUser();
		Map map = new HashMap();
		List list = new ArrayList();
		String printPath = "";
		PatMasterIndex patMasterIndex=patMasterIndexDao.get(patientId);
		ClinicMaster clinicMaster=clinicMasterDao.get(clinicId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
		SimpleDateFormat sdfyyr = new SimpleDateFormat("yyyy年MM月dd日");
		map.put("name",patMasterIndex.getName());//姓名
		map.put("idNo",patMasterIndex.getIdNo());//身份证号码
		map.put("address",patMasterIndex.getAddress());
		PlaceInfectionRecord placeInfectionRecord=super.get(id);
		if(id.length()!=0){
			map.put("infectionNo",placeInfectionRecord.getInfectionNo());//卡片编号
			map.put("genearchName",placeInfectionRecord.getGenearchName());//患儿家长姓名
			map.put("birthdayDate",sdfyyr.format(patMasterIndex.getBirthdayDate()));//出生日期
			map.put("realityAge",placeInfectionRecord.getRealityAge());//实际年龄
			map.put("workUnit",placeInfectionRecord.getWorkUnit());//工作单位托幼机构
			map.put("phone",placeInfectionRecord.getPhone());//电话
			map.put("illnessDate",sdfyyr.format(placeInfectionRecord.getIllnessDate()));//发病日期
			map.put("diagnoseDate",sdfyyr.format(placeInfectionRecord.getDiagnoseDate()));//诊断日期
			if(placeInfectionRecord.getDeathDate()!=null){
				map.put("deathDate",sdf.format(placeInfectionRecord.getDeathDate()));//死亡日期
			}else{
				map.put("deathDate",placeInfectionRecord.getDeathDate());//死亡日期
			}
			map.put("otherInfection",placeInfectionRecord.getOtherInfection());//其他传染病
			map.put("updateBeforeName",placeInfectionRecord.getUpdateBeforeName());//订正前病名
			map.put("returnReason",placeInfectionRecord.getReturnReason());//退卡原因
			map.put("reportUnit",placeInfectionRecord.getReportUnit());//报告单位
			map.put("unitPhone",placeInfectionRecord.getUnitPhone());//单位电话
			map.put("writeDate",sdfyyr.format(placeInfectionRecord.getWriteDate()));//填卡日期
			map.put("doctorName",placeInfectionRecord.getDoctorName());//报告医生
			map.put("remarks",placeInfectionRecord.getRemarks());//备注
		}
		//单选按钮列表
		List<Dict> reportTypeList = DictUtils.getDictList("REPORT_TYPE_DICT");//报告类型
		for(Dict dict:reportTypeList){
			if(dict.getValue().equals(placeInfectionRecord.getReportType())){
				map.put("reportType"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("reportType"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> correctTypeList = DictUtils.getDictList("CORRECT_TYPE_DICT");//订正报告类型
		for(Dict dict:correctTypeList){
			if(dict.getValue().equals(placeInfectionRecord.getCorrectType())){
				map.put("correctType"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("correctType"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> ageUnitList = DictUtils.getDictList("AGE_UNIT_DICT");//年龄单位
		for(Dict dict:ageUnitList){
			if(dict.getValue().equals(placeInfectionRecord.getAgeUnit())){
				map.put("ageUnit"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("ageUnit"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> areaList = DictUtils.getDictList("AREA_DICT");//病人区域
		for(Dict dict:areaList){
			if(dict.getValue().equals(placeInfectionRecord.getPatientArea())){
				map.put("area"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("area"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> identityList = DictUtils.getDictList("INFECTION_IDENTITY_DICT");//患者职业
		for(Dict dict:identityList){
			if(dict.getValue().equals(placeInfectionRecord.getInfectionIdentity())){
				map.put("identity"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("identity"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> caseTypeList = DictUtils.getDictList("CASE_TYPE_DICT");//病例分类
		for(Dict dict:caseTypeList){
			if(dict.getValue().equals(placeInfectionRecord.getCaseType())){
				map.put("caseType"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("caseType"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> speedTypeList = DictUtils.getDictList("SPEED_TYPE_DICT");//急性/慢性
		for(Dict dict:speedTypeList){
			if(dict.getValue().equals(placeInfectionRecord.getSpeedType())){
				map.put("speedType"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("speedType"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> aInfectionList = DictUtils.getDictList("A_INFECTION_DICT");//甲类传染病
		for(Dict dict:aInfectionList){
			if(dict.getValue().equals(placeInfectionRecord.getaInfection())){
				map.put("aInfection"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("aInfection"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> bInfectionList = DictUtils.getDictList("B_INFECTION_DICT");//乙类传染病
		for(Dict dict:bInfectionList){
			if(dict.getValue().equals(placeInfectionRecord.getbInfection())){
				map.put("bInfection"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("bInfection"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> cInfectionList = DictUtils.getDictList("C_INFECTION_DICT");//丙类传染病
		for(Dict dict:cInfectionList){
			if(dict.getValue().equals(placeInfectionRecord.getcInfection())){
				map.put("cInfection"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("cInfection"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		List<Dict> sexList = DictUtils.getDictList("SEX_DICT");//性别
		for(Dict dict:sexList){
			if(dict.getValue().equals(patMasterIndex.getSex())){
				map.put("sex"+dict.getValue(),"●"+dict.getLabel());
			}else{
				map.put("sex"+dict.getValue(),"○"+dict.getLabel());
			}
		}
		list.add("hello");
		list.add("cost");
		printPath = PrintPdfUtils.previewPdf(user.getId(), "his/printInfection.jasper", map, list, "printInfection");
		return printPath;
	}
}